//
//  ButtonCollectionViewCell.m
//  WWAVPlayer
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 LZY. All rights reserved.
//

#import "ButtonCollectionViewCell.h"
#import "Contents.h"
#import <UIButton+WebCache.h>
#import "Blocks.h"
#import <UIImageView+WebCache.h>
#import "MyScrollerView.h"
#import "ButtonTableViewCell.h"
#import "ButtonScrollerTableViewCell.h"
#import "ButtonFiveTableViewCell1.h"
#import "HorizontalTableViewCell.h"
#import "SliderTableViewCell.h"
#import "ButtonHeaderTableViewCell.h"
@interface ButtonCollectionViewCell()<UITableViewDataSource,UITableViewDelegate,ButtonTableViewCellDelegate,HorizontalTableViewCellDelegate,SliderTableViewCellDelegate,ButtonHeaderTableViewCellDelegate,ButtonFiveTableViewCellDelegate,ButtonScrollerTableViewCellDelegate>
@property (nonatomic,strong)NSMutableArray *buttonNameArr;
@property (nonatomic,strong)NSMutableArray *nameArr;
@property (nonatomic,strong)UIView *view1;
@property (nonatomic,strong)UILabel *label;


@end
@implementation ButtonCollectionViewCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)passToCollectionWithBlock:(Blocks *)block
{
    [self.delegate passToMainControllerWithBlock:block];
}


- (void)passToCollectionWithIdNum:(NSString *)idNum
{
    [self.delegate passToMainControllerWithIdNum:idNum];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {

        self.dataArr = [NSMutableArray array];
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height - 64 - 40) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.contentView addSubview:self.tableView];
        
        [self.tableView registerClass:[ButtonTableViewCell class] forCellReuseIdentifier:@"god"];
         [self.tableView registerClass:[HorizontalTableViewCell class] forCellReuseIdentifier:@"gun"];
        [self.tableView registerClass:[ButtonScrollerTableViewCell class] forCellReuseIdentifier:@"scroller"];
        [self.tableView registerClass:[ButtonFiveTableViewCell class] forCellReuseIdentifier:@"five"];
        [self.tableView registerClass:[SliderTableViewCell class] forCellReuseIdentifier:@"slider"];
        [self.tableView registerClass:[ButtonHeaderTableViewCell class] forCellReuseIdentifier:@"head"];
//        self.view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 30)];
//        _view1.backgroundColor = [UIColor redColor];
//        self.label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, Screen_Width, 30)];
//        Blocks *block = [self.dataArr objectAtIndex:section];
//        label.text = block.name;
//        [view1 addSubview:label];
//        [self.contentView addSubview:view1];
    }
    return self;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    Blocks *block = [self.dataArr objectAtIndex:section];
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 30 * H)];
        
        UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * W, 5 * H, Screen_Width / 2 - 10 * W, 20 * H)];
        
        headerLabel.text = block.name;
        
        headerLabel.font = [UIFont systemFontOfSize:15];
        
        [headerView addSubview:headerLabel];
        
        UIView *headerButtomView = [[UIView alloc] initWithFrame:CGRectMake(10,headerView.bounds.size.height - 1 * H, Screen_Width - 20, 1 * H)];
        
        headerButtomView.backgroundColor = [UIColor grayColor];
        
        [headerView addSubview:headerButtomView];
        
        if (![[block.channel objectForKey:@"id"] isEqualToString:@""]) {
            
            UIButton *headerButton = [UIButton buttonWithType:UIButtonTypeSystem];
            
            [headerButton setTitle:@"显示更多" forState:UIControlStateNormal];
            
            [headerButton addTarget:self action:@selector(headerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            
            headerButton.frame = CGRectMake(Screen_Width - 80 * W, 0, 80 * W, 30 * H);
            //            headerButton.backgroundColor = [UIColor grayColor];
            
            [headerView addSubview:headerButton];
        }
        return headerView;
    
}

- (void)headerButtonAction:(UIButton *)btn
{
    
}


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0)
{
    if(section == 0 || section == 1)
    {
        return 0;
    }else{
        return 30 * H;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return Screen_Height / 3 ;
    }else if(indexPath.section == 1){
        return 100;
    }else{
        Blocks *block = [self.dataArr objectAtIndex:indexPath.section];
        if ([block.template isEqualToString:@"still"])
        {
            return 125 * block.contents.count / 2;
        }else if([block.template isEqualToString:@"poster"]){
            
            return 165 * block.contents.count / 3;
          
        }else if([block.template isEqualToString:@"lposter"]){
            
            return 160 * (block.contents.count - 1) / 3 + 140;
        }else{
            
            return 125 ;
        }
    }
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    Blocks *block = [self.dataArr objectAtIndex:indexPath.section];
    if (indexPath.section == 0) {
        ButtonScrollerTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"scroller"];
        cell.backgroundColor = [UIColor redColor];
        
        cell.imageArr = [NSMutableArray array];
        cell.titleArr = [NSMutableArray array];
        for (NSDictionary *dic in block.contents) {
            NSString *titleStr = [dic objectForKey:@"name"];
            
            NSString *imageStr = [dic objectForKey:block.template];
            
            [cell.imageArr addObject:imageStr];
            
            [cell.titleArr addObject:titleStr];
        }
        
        cell.delegate = self;
        cell.block = block;
        
        [cell.myScrollerView.scrollView reloadData];
       
        return cell;
        
    }else if (indexPath.section == 1){
            ButtonFiveTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"five"];
        cell.buttonImageArr = [NSMutableArray array];
        cell.buttonNameArr = [NSMutableArray array];
        
        for (NSDictionary *dic in block.contents) {
            NSString *titleStr = [dic objectForKey:@"name"];
            
            NSString *imageStr = [dic objectForKey:@"icon"];
            
            [cell.buttonNameArr addObject:titleStr];
            
            [cell.buttonImageArr addObject:imageStr];
        }
        cell.delegate = self;
        [cell.collectionView reloadData];
        return cell;
    }else {
            if ([block.template isEqualToString:@"still"]) {
                HorizontalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"gun"];
                
                cell.block = block;
                cell.delegate = self;
                [cell.collectionView reloadData];
                return cell;
                
            }else if([block.template isEqualToString:@"poster"]){
                ButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"god"];
                
                cell.block = block;
                cell.delegate = self;
                [cell.collectionView reloadData];
                return cell;
                
            }else if([block.template isEqualToString:@"lposter"]){
                
                ButtonHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"head"];
                
                cell.block = block;
                
                cell.delegate = self;
                
                [cell.collectionView reloadData];
                return cell;
            }else{
                SliderTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:@"slider"];
                cell.block = block;
                cell.delegate = self;
                [cell.collectionView reloadData];
                return cell;
            }

        }
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    self.tableView.frame = CGRectMake(0, 0, Screen_Width, Screen_Height - 64 - 40);
    
}

@end
